package org.csu.hotelsys.controller;

import org.csu.hotelsys.domain.ConsumAccount;
import org.csu.hotelsys.domain.Consumption;
import org.csu.hotelsys.service.ConsumptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@RequestMapping("/consumption/")
@Controller
public class ConsumptionController {
    @Autowired
    private ConsumptionService consumptionService;

    @GetMapping("viewConsumption")//这个是在index界面点击消费管理时操作
    public String viewConsumption(Model model) {
        List<Consumption> consumptionList = consumptionService.getConsumptionList();
        model.addAttribute("consumptionList", consumptionList);
        return "consumption/consumption";
    }

    @GetMapping("deleteConsumption")//这个是在消费消费管理界面删除订单时的操作
    public String deleteConsumption(String listId, Model model) {
        Consumption consumption = consumptionService.getConsumption(listId);
        consumptionService.deleteConsumption(consumption);
        List<Consumption> consumptionList = consumptionService.getConsumptionList();
        model.addAttribute("consumptionList", consumptionList);
        return "consumption/consumption";
    }

    @GetMapping("updateConsumptionForm")//这个是点击每个订单的单号时的操作
    public String updateConsumptionForm(String listId, Model model) {//进入修改的界面
        Consumption consumption = consumptionService.getConsumption(listId);
        model.addAttribute("consumption", consumption);
        return "/consumption/update";
    }

    @GetMapping("updateConsumption")//这个是在修改订单信息点击修改时的操作
    public String updateConsumption(Consumption consumption, Model model) {
        consumptionService.updateConsumption(consumption);
        consumption = consumptionService.getConsumption(consumption.getListId());
        model.addAttribute("consumption", consumption);
        model.addAttribute("msg", "修改成功");
        return "consumption/update";
    }

    @GetMapping("entry")//下面是在点击入账时的操作：首先费用入账，其次库存相应商品数量减少。入账是将客户消费商品的费用加到其账单里
    public String entryGoods(String listId, Model model) {
        Consumption consumption = consumptionService.getConsumption(listId);
        String checkInId = consumption.getCheckInId();
        ConsumAccount consumAccount = consumptionService.getConsumAccount(checkInId);//
        consumAccount.setTotal(consumption.getTotal());
        consumptionService.updateConsumAccount(consumAccount);
        double Total = consumptionService.getConsumAccount(checkInId).getTotal();
        consumptionService.deleteConsumption(consumption);//删除当前订单,因为已经入账
        List<Consumption> consumptionList = consumptionService.getConsumptionList();
//        model.addAttribute("checkInId",checkInId);
//
        model.addAttribute("consumAccount", consumAccount);
        model.addAttribute("Total", Total);
        model.addAttribute("checkInId", checkInId);
        model.addAttribute("consumptionList", consumptionList);

        return "consumption/consumption";
    }

}
